package com.caipin.service.impl;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:32
 * @Description: TODO
 */
@Service
public class TemperatureStatisticalResultServiceImpl implements TemperatureStatisticalResultService {
    @Autowired
    private GateComparisonResultDao gateComparisonResultDao;

    public PageBean<StudentTemperature> getStudentTemperatureStatisticalResultList(Integer pageNum, Integer pageSize, String startTime, String endTime, String grade, String clbum) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long start = sdf.parse(startTime).getTime() / 1000L * 1000L;
            long end = sdf.parse(endTime).getTime() / 1000L * 1000L;
            Page<GateComparisonResult> studentTemperaturePage = new Page(pageNum.intValue(), pageSize.intValue());
            QueryWrapper<GateComparisonResult> queryWrapper = new QueryWrapper();
            queryWrapper.between("screen_time", new Date(start), new Date(end));
            if (null != grade)
                queryWrapper.eq("grade", grade);
            if (null != clbum)
                queryWrapper.eq("clbum", clbum);
            IPage<GateComparisonResult> page = this.gateComparisonResultDao.selectPage((IPage)studentTemperaturePage, (Wrapper)queryWrapper);
            PageBean<StudentTemperature> pageBean = new PageBean();
            pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
            pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
            pageBean.setTotalPage(Long.valueOf(page.getPages()));
            pageBean.setTotalCount(Long.valueOf(page.getTotal()));
            List<StudentTemperature> studentTemperatureList = new ArrayList<>();
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (GateComparisonResult gateComparisonResult : page.getRecords()) {
                StudentTemperature studentTemperature = (new StudentTemperature()).setId(gateComparisonResult.getId()).setDate(sdf.format(gateComparisonResult.getScreenTime())).setName(gateComparisonResult.getUsername()).setGender(gateComparisonResult.getGender()).setGrade(gateComparisonResult.getGrade()).setClbum(gateComparisonResult.getClbum()).setMorning(gateComparisonResult.getTemperature()).setMidday(gateComparisonResult.getAfterTemperature());
                studentTemperatureList.add(studentTemperature);
            }
            pageBean.setList(studentTemperatureList);
            return pageBean;
        } catch (Throwable $ex) {
            throw $ex;
        }
    }

    public void updateStudentTemperatureStatisticalResult(StudentTemperature studentTemperature) {
        GateComparisonResult gateComparisonResult = (GateComparisonResult)this.gateComparisonResultDao.selectById(studentTemperature.getId());
        gateComparisonResult.setTemperature(studentTemperature.getMorning()).setAfterTemperature(studentTemperature.getMidday());
        this.gateComparisonResultDao.updateById(gateComparisonResult);
    }

    public String getStudentTemperatureStatisticalResultListFile(Date startTime, Date endTime, String grade, String clbum) {
        QueryWrapper<GateComparisonResult> queryWrapper = new QueryWrapper();
        queryWrapper.between("screen_time", startTime, endTime);
        if (null != grade)
            queryWrapper.eq("grade", grade);
        if (null != clbum)
            queryWrapper.eq("clbum", clbum);
        List<GateComparisonResult> gateComparisonResultList = this.gateComparisonResultDao.selectList((Wrapper)queryWrapper);
        List<StudentTemperatureExcel> studentTemperatureExcelList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (GateComparisonResult gateComparisonResult : gateComparisonResultList)
            studentTemperatureExcelList.add((new StudentTemperatureExcel())

                    .setDate(sdf.format(gateComparisonResult.getScreenTime()))
                    .setName(gateComparisonResult.getUsername())
                    .setGender(gateComparisonResult.getGender())
                    .setGrade(gateComparisonResult.getGrade())
                    .setClbum(gateComparisonResult.getClbum())
                    .setMorning(gateComparisonResult.getTemperature())
                    .setMidday(gateComparisonResult.getAfterTemperature()));
        String filename = "exportStudentTemperature" + System.currentTimeMillis() + ".xls";
        String[] headers = { ", ", ", ", ", ", "};
                ExportExcel.exportExcel("sheet0", ", headers, studentTemperatureExcelList, filename, "");
        return filename;
  }
    }

